package com.xiaolei.lib.annotation.translators;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.xiaolei.lib.Activity.IActivity;
import com.xiaolei.lib.Activity.bean.AnnotationWrape;
import com.xiaolei.lib.annotation.Bind;
import com.xiaolei.lib.annotation.findViewById;
import com.xiaolei.lib.core.ITranslator;

import android.view.View;

/**
 * @findViewById 的注解解释器
 * @author xiaolei
 */
public class FindViewByIdTranslator implements ITranslator
{

	@Override
	public void execute(Field field, IActivity activity, AnnotationWrape annotation)
	{
		int id = -1;

		findViewById fById = annotation.get(findViewById.class);
		Bind bind = annotation.get(Bind.class);
		
		if (fById != null)
		{
			id = fById.value();
		}else if (bind != null) 
		{
			id = bind.value();
		}
		
		View view = activity.findViewByID(id);
		try
		{
			if (field.getType().isInstance(view))
			{
				if (!field.isAccessible())
				{
					field.setAccessible(true);
				}
				field.set(activity, view);
			}
		} catch (IllegalAccessException e)
		{
			e.printStackTrace();
		} catch (IllegalArgumentException e)
		{
			e.printStackTrace();
		}

	}

	@Override
	public void execute(Method method, IActivity activity, AnnotationWrape annotation)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void execute(Class<?> type, IActivity activity, AnnotationWrape annotation)
	{
		// TODO Auto-generated method stub

	}

}
